﻿using AutoMapper;
using FluentValidation;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    [ApiController]
    [Route("/api/v1")]
    public class MeasuringUnitTypeController : ControllerBase
    {
        private readonly ILogger<MeasuringUnitTypeController> _logger;
        private readonly IMeasuringUnitTypeService _measuringUnitTypeService;
        private readonly IMapper _mapper;
        private readonly IValidator<MeasuringUnitTypeInput> _measuringUnitTypeInputValidator;
        private readonly IValidator<MeasuringUnitTypeUpdateInput> _measuringUnitTypeUpdateInputValidator;
        private readonly IValidator<MeasuringUnitTypePageInput> _measuringUnitTypePageInputValidator;

        public MeasuringUnitTypeController(ILogger<MeasuringUnitTypeController> logger,
            IMeasuringUnitTypeService measuringUnitTypeService,
            IMapper mapper,
            IValidator<MeasuringUnitTypeInput> measuringUnitTypeInputValidator,
            IValidator<MeasuringUnitTypeUpdateInput> measuringUnitTypeUpdateInputValidator,
            IValidator<MeasuringUnitTypePageInput> measuringUnitTypePageInputValidator
            )
        {
            _logger = logger;
            _measuringUnitTypeService = measuringUnitTypeService;
            _mapper = mapper;
            _measuringUnitTypeInputValidator = measuringUnitTypeInputValidator;
            _measuringUnitTypeUpdateInputValidator = measuringUnitTypeUpdateInputValidator;
            _measuringUnitTypePageInputValidator = measuringUnitTypePageInputValidator;
        }

        /// <summary>
        /// 添加计量单位类型
        /// </summary>
        /// <param name="measuringUnitTypeInput"></param>
        /// <returns></returns>
        [HttpPost("measuringunittypes")]
        public async Task<ActionResult> AddMeasuringUnitType(MeasuringUnitTypeInput measuringUnitTypeInput)
        {
            try
            {
                var input = await _measuringUnitTypeInputValidator.ValidateAsync(measuringUnitTypeInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _measuringUnitTypeService.AddMeasuringUnitTypeAsync(measuringUnitTypeInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 修改计量单位类型
        /// </summary>
        /// <param name="measuringUnitTypeUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("measuringunittypes")]
        public async Task<ActionResult> UpdateMeasuringUnitType(MeasuringUnitTypeUpdateInput measuringUnitTypeUpdateInput)
        {
            try
            {
                var input = await _measuringUnitTypeUpdateInputValidator.ValidateAsync(measuringUnitTypeUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _measuringUnitTypeService.UpdateMeasuringUnitTypeAsync(measuringUnitTypeUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除计量单位类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("measuringunittypes")]
        public async Task<ActionResult> DeleteMeasuringUnitType(int[] ids)
        {
            try
            {
                await _measuringUnitTypeService.DeleteMeasuringUnitTypeAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询计量单位类型
        /// </summary>
        /// <param name="measureUnitTypePageInput"></param>
        /// <returns>PagedItemsData<MeasuringUnitTypeResult></returns>
        [HttpGet("measuringunittypes")]
        public async Task<ActionResult> GetMeasuringUnitTypes([FromQuery] MeasuringUnitTypePageInput measureUnitTypePageInput)
        {
            try
            {
                var input = await _measuringUnitTypePageInputValidator.ValidateAsync(measureUnitTypePageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _measuringUnitTypeService.GetMeasuringUnitTypesAsync(measureUnitTypePageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号查询计量单位类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MeasuringUnitTypeResult</returns>
        [HttpGet("measuringunittypes/entity")]
        public async Task<ActionResult> GetMeasuringUnitTypeEntityById(int id)
        {
            try
            {
                var result = await _measuringUnitTypeService.GetMeasuringUnitTypeAsync(id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
